/**
 * 判断字段是否是候选值类型的
 * @param field
 * @returns
 */
export const isCandidateField = (field: any) => {
  const showType = field.showType;
  return showType == "select" || showType == "checkbox" || showType == "radio" || showType == "cascaderselect";
};

export const getAllVisualization = (DetailData: any) => {
  let functionGroups = DetailData.functionGroups;
  let functionGroupFields = DetailData.functionGroupFields;

  let allFields: Array<any> = [];
  // functionGroupFields.filter((item: any) => item.groupType == 'main').forEach((item: any) => {
  //     item.id = item.fieldId
  //     allFields.push(item)
  // })

  functionGroupFields.forEach((item: any) => {
    item.id = item.fieldId;
    if (item.groupType == "main") {
      allFields.push(item);
    }
  });

  functionGroups
    .filter((item: any) => item.groupType == "childlist" || item.groupType == "relationquery")
    .forEach((item: any) => {
      item.id = item.groupId;
      item.fields = functionGroupFields.filter((m: any) => m.groupId == item.groupId);
      allFields.push(item);
    });
  return allFields.sort((m: any, m1: any) => {
    return m.orderNum - m1.orderNum;
  });
};

// 为了更好的生成唯一的 ID，需要获取当前时间戳
function getTimestamp() {
  return new Date().getTime();
}

// 生成一个随机的工作机器 ID，实际应用中应该使用机器的真实标识符
function getWorkerID() {
  return Math.floor(Math.random() * 100000);
}

// 生成一个随机的序列号
function getSequence() {
  return Math.floor(Math.random() * 100000);
}

// 生成雪花 ID 的主函数
export const generateSnowflakeID = () => {
  const timestamp = getTimestamp();
  const workerID = getWorkerID();
  const sequence = getSequence();

  // 将时间戳、工作机器 ID 和序列号组合在一起生成雪花 ID
  return `${timestamp}${workerID}${sequence}`;
};

export const randomCoding = (count: number) => {
  //创建26个字母数组
  var arr = [
    "A",
    "B",
    "C",
    "D",
    "E",
    "F",
    "G",
    "H",
    "I",
    "J",
    "K",
    "L",
    "M",
    "N",
    "O",
    "P",
    "Q",
    "R",
    "S",
    "T",
    "U",
    "V",
    "W",
    "X",
    "Y",
    "Z"
  ];
  var idvalue = "";

  for (var i = 0; i < count; i++) {
    idvalue += arr[Math.floor(Math.random() * 26)];
  }
  return idvalue.toLowerCase();
};

/**
 * 获取需要在列表上显示的字段
 */
export const getShowListFields = (functionGroupFields: Array<any>) => {
  return functionGroupFields.filter(item => item.groupType == "main" && item.showList);
};

/**
 * 获取在列表上可以用于查询的字段数组
 * @param functionGroupFields
 * @returns
 */
export const getQueryConditionFields = (functionGroupFields: Array<any>) => {
  return functionGroupFields.filter(item => item.queryCondition);
};

/**
 * 将后端返回配置数据转换成预览需要的数据结构
 */
export const convertPreviewDataByServerDetail = (functionDetailVo: any) => {
  let functionGroupFields: Array<any> = functionDetailVo.functionGroupFields;
  let functionGroups: Array<any> = functionDetailVo.functionGroups;

  let functionPreviewVo = {
    function: functionDetailVo.function,
    showListFields: getShowListFields(functionGroupFields),
    queryConditionFields: getQueryConditionFields(functionGroupFields),
    groups: [] as Array<any>
  };
  let groups: Array<any> = [];
  functionGroups.forEach((functionGroup: any) => {
    let group = { fields: [] as Array<any>, group: {} as any };
    let fields: Array<any> = functionGroupFields.filter((item: any) => item.groupId == functionGroup.groupId);
    group.fields = fields;
    group.group = functionGroup;
    groups.push(group);
  });
  functionPreviewVo.groups = groups;
  return functionPreviewVo;
};

/**
 *
 * old:
 * 将前端未保存前的数据转换成预览需要的数据结构（即前端模拟下后端的返回数据）
 */
export const convertPreviewDataByTemp = (masterDetail2: any) => {
  /**
   * 未保存的数据结构：
   * {
   *  detail1: {
   *      insert: [],
   *      update: [],
   *      delete: []
   *  },
   *  detail2: {
   *      insert: [],
   *      update: [],
   *      delete: []
   *  },
   *  master: {
   *  },
   *  status: 'update'
   * }
   */

  /**
   * 转换后的数据结构：
   * {
   *   function: {},
   *   functionGroupFields: [],
   *   functionGroups: [],
   *   queryConditionFields: []
   * }
   */

  let detailData = {
    function: masterDetail2.master,
    functionGroupFields: masterDetail2.detail2.insert.concat(masterDetail2.detail2.update),
    functionGroups: masterDetail2.detail1.insert.concat(masterDetail2.detail1.update)
  };

  /**最终再转换成预览需要的数据 */
  return convertPreviewDataByServerDetail(detailData);
};
